package com.ueffort.social.platform;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.SyncStateContract;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.ueffort.social.SocialProvider;
import com.ueffort.social.SocialSDK;
import com.ueffort.social.action.AuthAction;
import com.ueffort.social.action.RealAction;
import com.ueffort.social.action.ShareAction;
import com.ueffort.social.action.LeaderboardAction;
import com.ueffort.social.action.AchievementAction;
import com.ueffort.social.action.SocialAction;
import com.ueffort.social.inter.ShareObject;

import java.util.HashMap;
import java.util.Map;

public class WechatSocialProvider implements SocialProvider{
    private static WechatSocialProvider instance=new WechatSocialProvider();

    public IWXAPI api;
    private String appId;

    private AuthAction authAction;
    private ShareAction shareAction;

    public static WechatSocialProvider Instance(){
        return instance;
    }

    private WechatSocialProvider(){
    }

    @Override
    public void Init(String appId, Activity activity, SocialAction action){
        api = WXAPIFactory.createWXAPI(activity, appId, false);
        api.registerApp(appId);
    }

    public void onAuth(SendAuth.Resp resp){
        if (authAction == null) return;

        Log.e("wechat", String.valueOf(resp.errCode));
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                HashMap<String, String> data = new HashMap<>();
                data.put("code", resp.code);
                data.put("state", "");
                authAction.onComplete(SocialSDK.SocialPlatformType.Wechat,  data);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                authAction.onCancel(SocialSDK.SocialPlatformType.Wechat);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                authAction.onError(SocialSDK.SocialPlatformType.Wechat, new Throwable("Auth Denied") );
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                authAction.onError(SocialSDK.SocialPlatformType.Wechat, new Throwable("un support") );
                break;
            default:
                break;
        }
        authAction = null;
    }

    public void onShare(SendMessageToWX.Resp resp){
        if (shareAction == null) return ;
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                shareAction.onResult(SocialSDK.SocialPlatformType.Wechat);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                shareAction.onCancel(SocialSDK.SocialPlatformType.Wechat);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                shareAction.onError(SocialSDK.SocialPlatformType.Wechat, new Throwable("Auth Denied"));
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                shareAction.onError(SocialSDK.SocialPlatformType.Wechat, new Throwable("un support") );
                break;
            default:
                break;
        }
        shareAction = null;
    }

    @Override
    public boolean isInstall() {
        if (api == null)
        {
            return false;
        } else {
            return api.isWXAppInstalled();
        }
    }

    @Override
    public boolean isShared() {
        return true;
    }

    @Override
    public boolean isLeaderboard() {
        return false;
    }

    @Override
    public boolean isAchievement() {
        return false;
    }

    @Override
    public boolean isReal() {
        return false;
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        return false;
    }

    @Override
    public void onCreate(Activity activity) {

    }

    @Override
    public void onDestroy(Activity activity) {

    }

    @Override
    public void Authorize(AuthAction action) {
        authAction = action;
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "";
        api.sendReq(req);
    }

    @Override
    public void AuthorizeStatus(AuthAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Wechat);
    }

    @Override
    public void UpdateData(Map<String, String> info) {

    }

    @Override
    public void Real(RealAction action) {
    }

    @Override
    public void ShareWithObject(ShareObject share, ShareAction action) {
        shareAction = action;
        WXTextObject textObj = new WXTextObject();
        textObj.text = share.title;

        WXImageObject imageObject = new WXImageObject();


        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObj;
        msg.title = share.title;
        msg.description = share.description;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        api.sendReq(req);
    }

    @Override
    public void Logout(){

    }

    @Override
    public void SubmitLeaderboard(String key, int value, LeaderboardAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Wechat);
    }

    @Override
    public void AddAchievement(String key, int value, AchievementAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Wechat);
    }
}
